import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider

# 创建一些数据
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0 * np.sin(2 * np.pi * f0 * t)

# 创建图形和轴
fig, ax = plt.subplots()
fig.set_size_inches(10, 5)  # 设置图形的宽度是高度的一倍
plt.subplots_adjust(bottom=0.25)  # 调整子图位置以给滑动条留出空间
l, = plt.plot(t, s, lw=2)

# 创建滑动条轴
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)

# 定义滑动条更新函数
def update(val):
    freq = sfreq.val
    l.set_ydata(a0 * np.sin(2 * np.pi * freq * t))
    fig.canvas.draw_idle()

# 将更新函数绑定到滑动条上
sfreq.on_changed(update)

plt.show()